package com.epam.web_project.bank.command.customer_commands;

import java.io.IOException;
import java.sql.Date;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.epam.web_project.bank.command.Command;
import com.epam.web_project.bank.dao.customer_dao.CustomerDAO;
import com.epam.web_project.bank.dao.factory.MySQLDAOFactory;
import com.epam.web_project.bank.db.ConnectionPool;
import com.epam.web_project.bank.entity.Customer;
import com.epam.web_project.bank.exception.RegistrationException;
import com.epam.web_project.bank.util.AppPropertiesLoader;
import com.epam.web_project.bank.util.PasswordEncoder;

public class RegisterCommand implements Command {

	private static Logger logger = Logger.getLogger(RegisterCommand.class);

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException, RegistrationException {
		String firstName = request.getParameter("first_name");
		String lastName = request.getParameter("last_name");
		String dob = request.getParameter("dob");
		String address = request.getParameter("address");
		String email = request.getParameter("email");
		String gender = request.getParameter("gender");
		String passpNum = request.getParameter("passp");
		String iin = request.getParameter("iin");
		if("".equals(iin)) {
			iin=null;
		}
		String telephone = request.getParameter("telephone");
		String login = request.getParameter("login");
		String plain_password = request.getParameter("password");
		String password = PasswordEncoder.sha2Encode(plain_password);
		String question = request.getParameter("question");
		String answer = request.getParameter("answer");

		Customer customer = new Customer();
		customer.setAddress(address);
		customer.setAnswer(answer);
		Date dateOfBirth = Date.valueOf(dob);
		customer.setDateOfBirth(dateOfBirth);
		customer.setEmail(email);
		customer.setGender(gender);
		customer.setIin(iin);
		customer.setLogin(login);
		customer.setFirstName(firstName);
		customer.setLastName(lastName);
		customer.setPassportNumber(passpNum);
		customer.setPassword(password);
		customer.setQuestion(question);
		customer.setTelephone(telephone);
		customer.setRole("customer");
		customer.setOnApproval(true);

		String resPage;
		MySQLDAOFactory daoFactory = new MySQLDAOFactory();
		daoFactory.setConnectionPool(ConnectionPool.getInstance());
		CustomerDAO customerDAO = daoFactory.getCustomerDAO();
		AppPropertiesLoader propLoader = AppPropertiesLoader.getInstance();
		Locale locale=request.getLocale();
		ResourceBundle bundle=ResourceBundle.getBundle("i18n", locale);
		int userId = customerDAO.save(customer);
		if (userId != -1) {
			customer.setId(userId);
			HttpSession session = request.getSession(true);
			session.setAttribute("customer", customer);
			resPage = propLoader.getProperty("show_card_request_command");
		} else {
			logger.warn(bundle.getString("java.reg_failed"));
			throw new RegistrationException(
					bundle.getString("java.reg_failed"));
		}
		return resPage;
	}

}
